package com.wd.gui;

import com.wd.dominio.CentroDistribucion;
import com.wd.dominio.EmpresaVigilancia;
import com.wd.dominio.Lugar;
import com.wd.dominio.Servicio;
import com.wd.dominio.Tienda;
import com.wd.gui.controlparticular.ControlGuiCentroDistribucion;
import com.wd.gui.controlparticular.ControlGuiEmpresaVigilancia;
import com.wd.gui.controlparticular.ControlGuiLugar;
import com.wd.gui.controlparticular.ControlGuiTienda;
import java.util.Vector;

/**
 *
 * @author Gabylis
 */
public class VentanaConsultarEmpresaVigilancia extends javax.swing.JFrame {

    private Vector<Lugar> estados;

    private Vector<Lugar> ciudades;

    private ControlGui controlGeneral;

    private Vector<Lugar> ciudadesAux;

    private Vector<CentroDistribucion> vecCentros;

    private Vector <Tienda> vecTiendas;

    private Vector <EmpresaVigilancia> vecEmps;

    private Vector <EmpresaVigilancia> vecEmpsAux;

    private ControlGuiLugar controlLugar;

    private ControlGuiCentroDistribucion controlCentro;

    private ControlGuiTienda controlTienda;

    private ControlGuiEmpresaVigilancia controlEmpresa;

    /** Creates new form VentanaConsultarEmpresaVigilancia */
    public VentanaConsultarEmpresaVigilancia(Vector<Lugar> result) {
        initComponents();
        java.net.URL url = getClass().getResource("Iconos/icon_016.png");
        java.awt.Image imagen = getToolkit().getImage(url);
        setIconImage (imagen);

        vecTiendas = new Vector();
        vecCentros = new Vector();
        vecEmps = new Vector();
        vecEmpsAux = new Vector();
        ciudadesAux = new Vector();
        ciudades = new Vector();
        estados = new Vector();

        controlEmpresa = new ControlGuiEmpresaVigilancia();
        vecEmps = controlEmpresa.traerTodasLasEmpresas();

        controlCentro = new ControlGuiCentroDistribucion();
        vecCentros = controlCentro.traerTodosLosCentros();

        controlTienda = new ControlGuiTienda();
        vecTiendas = controlTienda.consultarTiendas();

        estados = result;

        for (Lugar lugar : result){
            this.comboEstado.addItem(lugar.getNombrePropio());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        comboEstado = new javax.swing.JComboBox();
        comboCiudad = new javax.swing.JComboBox();
        comboNombre = new javax.swing.JComboBox();
        campoRif = new javax.swing.JTextField();
        campoTelefono = new javax.swing.JTextField();
        campoNombrePer = new javax.swing.JTextField();
        campoApellidoPer = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        campoDireccion = new javax.swing.JTextArea();
        comboTiendas = new javax.swing.JComboBox();
        comboCentros = new javax.swing.JComboBox();
        buttonCerrar = new javax.swing.JButton();
        labelEstado = new javax.swing.JLabel();
        labelCiudad = new javax.swing.JLabel();
        labelNombre = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        labelTelefono = new javax.swing.JLabel();
        labelNombrePer = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        labelDireccion = new javax.swing.JLabel();
        labelTiendas = new javax.swing.JLabel();
        labelCentros = new javax.swing.JLabel();

        setTitle("Consultar Empresa de Vigilancia");
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Consultar Empresa de Vigilancia", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(0, 102, 204))); // NOI18N

        comboEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboEstadoActionPerformed(evt);
            }
        });

        comboCiudad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCiudadActionPerformed(evt);
            }
        });

        comboNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboNombreActionPerformed(evt);
            }
        });

        campoRif.setEditable(false);

        campoTelefono.setEditable(false);

        campoNombrePer.setEditable(false);

        campoApellidoPer.setEditable(false);

        campoDireccion.setColumns(20);
        campoDireccion.setEditable(false);
        campoDireccion.setFont(new java.awt.Font("Tahoma", 0, 13));
        campoDireccion.setRows(5);
        jScrollPane1.setViewportView(campoDireccion);

        buttonCerrar.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonCerrar.setText("Cerrar");

        labelEstado.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelEstado.setText("Estado");

        labelCiudad.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelCiudad.setText("Ciudad");

        labelNombre.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelNombre.setText("Empresa de Vigilancia");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("RIF");

        labelTelefono.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelTelefono.setText("Teléfono");

        labelNombrePer.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelNombrePer.setText("Nombre Persona Contacto");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Apellido Persona Contacto");

        labelDireccion.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelDireccion.setText("Dirección");

        labelTiendas.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelTiendas.setText("Tiendas");

        labelCentros.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelCentros.setText("Centros De Distribución");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelCiudad)
                            .addComponent(labelEstado)
                            .addComponent(labelNombre)
                            .addComponent(jLabel3)
                            .addComponent(labelTelefono)
                            .addComponent(labelNombrePer)
                            .addComponent(jLabel1)
                            .addComponent(labelDireccion)
                            .addComponent(labelTiendas)
                            .addComponent(labelCentros))
                        .addGap(61, 61, 61)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(comboCentros, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(comboCiudad, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(comboEstado, javax.swing.GroupLayout.Alignment.LEADING, 0, 229, Short.MAX_VALUE)
                            .addComponent(comboNombre, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(campoRif, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(campoTelefono, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(campoNombrePer, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(campoApellidoPer, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboTiendas, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(131, 131, 131)
                        .addComponent(buttonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 203, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelEstado)
                    .addComponent(comboEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCiudad)
                    .addComponent(comboCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNombre)
                    .addComponent(comboNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(campoRif, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTelefono)
                    .addComponent(campoTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelNombrePer)
                    .addComponent(campoNombrePer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(campoApellidoPer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelDireccion)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboTiendas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelTiendas))
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboCentros, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCentros))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 15, Short.MAX_VALUE)
                .addComponent(buttonCerrar)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboEstadoActionPerformed
        int select = -1;
        select = this.comboEstado.getSelectedIndex();
        Lugar estadoDueño = new Lugar();
        estadoDueño = estados.elementAt(select);
        cargarCiudades(estadoDueño);
    }//GEN-LAST:event_comboEstadoActionPerformed

    private void comboCiudadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCiudadActionPerformed
        int select = -1;
        int idCiudad = -1;
        if (this.comboCiudad.getItemCount()>0){
            select = this.comboCiudad.getSelectedIndex();
            idCiudad = this.ciudadesAux.elementAt(select).getId();
            this.cargarComboEmpresa(idCiudad);
        }
    }//GEN-LAST:event_comboCiudadActionPerformed

    private void comboNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboNombreActionPerformed
        int select = -1;
        String rif = "";
        String telefono = "";
        String nombrePer = "";
        String apellidoPer = "";
        String direccion = "";

        if (this.comboNombre.getItemCount()>0){
            select = this.comboNombre.getSelectedIndex();
            EmpresaVigilancia auxEmp = new EmpresaVigilancia();
            auxEmp = this.vecEmpsAux.elementAt(select);
            rif = auxEmp.getRif();
            telefono = auxEmp.getTelefono();
            nombrePer = auxEmp.getNombreContacto();
            apellidoPer = auxEmp.getApellidoContacto();
            direccion = auxEmp.getDireccion();

            this.campoRif.setText(rif);
            this.campoTelefono.setText(telefono);
            this.campoNombrePer.setText(nombrePer);
            this.campoApellidoPer.setText(apellidoPer);
            this.campoDireccion.setText(direccion);

            this.cargarComboTiendas(rif);
            this.cargarComboCentros(rif);
        }
    }//GEN-LAST:event_comboNombreActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run(Vector<Lugar> result) {
                new VentanaConsultarEmpresaVigilancia(result).setVisible(true);
            }
            public void run() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCerrar;
    private javax.swing.JTextField campoApellidoPer;
    private javax.swing.JTextArea campoDireccion;
    private javax.swing.JTextField campoNombrePer;
    private javax.swing.JTextField campoRif;
    private javax.swing.JTextField campoTelefono;
    private javax.swing.JComboBox comboCentros;
    private javax.swing.JComboBox comboCiudad;
    private javax.swing.JComboBox comboEstado;
    private javax.swing.JComboBox comboNombre;
    private javax.swing.JComboBox comboTiendas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelCentros;
    private javax.swing.JLabel labelCiudad;
    private javax.swing.JLabel labelDireccion;
    private javax.swing.JLabel labelEstado;
    private javax.swing.JLabel labelNombre;
    private javax.swing.JLabel labelNombrePer;
    private javax.swing.JLabel labelTelefono;
    private javax.swing.JLabel labelTiendas;
    // End of variables declaration//GEN-END:variables

    public void cargarCiudades(Lugar Estado){
        this.comboCiudad.removeAllItems();
        this.ciudadesAux.removeAllElements();

        int codigoEstado = -1;
        codigoEstado = Estado.getId();

        controlLugar = new ControlGuiLugar();
        ciudades = controlLugar.traerTodosLosLugares(2);

        for (Lugar lugar : ciudades) {
            if (lugar.getLugarID() == codigoEstado){
                ciudadesAux.addElement(lugar);
                this.comboCiudad.addItem(lugar.getNombrePropio());
            }
        }
    }

    public void cargarComboEmpresa(int idCiudad){
        this.comboNombre.removeAllItems();
        this.vecEmpsAux.removeAllElements();

        for (EmpresaVigilancia emp : vecEmps){
            if (emp.getLugarId() == idCiudad){
                this.vecEmpsAux.addElement(emp);
                this.comboNombre.addItem(emp.getNombre());
            }
        }
        if (this.comboNombre.getItemCount() == 0){
            this.comboCentros.removeAllItems();
            this.comboTiendas.removeAllItems();
            this.campoRif.setText("");
            this.campoTelefono.setText("");
            this.campoNombrePer.setText("");
            this.campoApellidoPer.setText("");
            this.campoDireccion.setText("");
        }
    }

    public void cargarComboTiendas(String rif){
        this.comboTiendas.removeAllItems();
        for (Tienda tienda : vecTiendas){
            if (tienda.getEMPRESA_SERVICIO_rif().equals(rif)){
                this.comboTiendas.addItem(tienda.getNombre());
            }            
        }
        if (this.comboTiendas.getItemCount() == 0){
            this.comboTiendas.addItem("0 tiendas asociadas");
        }
    }

    public void cargarComboCentros(String rif){
        this.comboCentros.removeAllItems();
        int idCentro = -1;
        Vector<Servicio> servicios = new Vector();

        servicios = this.controlEmpresa.traerTodosLosServicios(rif);
        System.out.println("size-->"+servicios.size());

        for (Servicio servicio : servicios) {
            idCentro = servicio.getCentroDistribucionCodigo();
            for (CentroDistribucion centro : vecCentros){
                if (centro.getCodigo() == idCentro){
                    this.comboCentros.addItem(centro.getNombre());
                }
            }
        }
        if (this.comboCentros.getItemCount() == 0 ){
            this.comboCentros.addItem("0 Centros Asociados a la empresa");
        }
    }
}

